#!/bin/bash

# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function prepare_depot_tools() {
    if [ ! -d "depot_tools" ]
    then
        git clone https://gitee.com/getmoon/depot_tools.git
        if [ $? -ne 0 ]
        then
            echo "clone depot_tools failed!"
            rm -rf depot_tools
            return 1
        fi
    fi

    return 0
}

function prepare_flutter_engine_src() {
    if [ ! -d "flutter_engine_src" ]
    then
        git clone https://gitee.com/getmoon/flutter_engine_src.git
        if [ $? -ne 0 ]
        then
            echo "clone flutter_engine_src failed!"
            rm -rf flutter_engine_src
            return 1
        fi
    fi
	
    cp -ar flutter_engine_src/. src/
    cp -ar third_party/flutter/engine/. src/flutter/

    return 0
}

function prepare_flutter_engine_third_party() {
    if [ ! -d "flutter_engine_third_party" ]
    then
        git clone https://gitee.com/getmoon/flutter_engine_third_party.git
        if [ $? -ne 0 ]
        then
            echo "clone flutter_engine_third_party failed!"
            rm -rf flutter_engine_third_party
            return 1
        fi
    fi
	
    cp -ar flutter_engine_third_party/. src/third_party/

    return 0
}

function prepare_flutter_git_bak() {
    if [ ! -d "flutter_git_bak" ]
    then
        git clone https://gitee.com/getmoon/flutter_git_bak.git
        if [ $? -ne 0 ]
        then
            echo "clone flutter_git_bak failed!"
            rm -rf flutter_git_bak
            return 1
        fi
    fi
	
    unzip -q flutter_git_bak/dart.git.zip -d src/third_party/dart
    unzip -q flutter_git_bak/angle.git.zip -d src/third_party/angle
    unzip -q flutter_git_bak/skia.git.zip -d src/third_party/skia
    unzip -q flutter_git_bak/zlib.git.zip -d src/third_party/zlib
    unzip -q flutter_git_bak/libcxx.git.zip -d src/third_party/libcxx
    unzip -q flutter_git_bak/spirv-headers.git.zip -d src/third_party/vulkan-deps/spirv-headers/src

    return 0
}

function prepare_flutter_vpython() {
    if [ ! -d "flutter-vpython" ]
    then
        git clone https://gitee.com/getmoon/flutter-vpython.git
        if [ $? -ne 0 ]
        then
            echo "clone flutter-vpython failed!"
            rm -rf flutter-vpython
            return 1
        fi
    fi
	
    rm -rf .vpython_cipd_cache
    rm -rf .vpython-root
    cp -ar flutter-vpython/.vpython_cipd_cache/. .vpython_cipd_cache/
    cp -ar flutter-vpython/.vpython-root/. .vpython-root/	

    return 0
}

function prepare_flutter_engine_patch() {
    if [ ! -d "flutter_engine_patch" ]
    then
        git clone https://gitee.com/getmoon/flutter_engine_patch.git
        if [ $? -ne 0 ]
        then
            echo "clone flutter_engine_patch failed!"
            rm -rf flutter_engine_patch
            return 1
        fi
    fi
	
    rm -rf flutter_engine_patch/.git
    chmod 777 cicd/flutter/engine/copy_files.sh
    cicd/flutter/engine/copy_files.sh flutter_engine_patch/ src/flutter/

    return 0
}

function prepare_flutter_env() {
    if [ ! -d "flutter_env" ]
    then
        git clone https://gitee.com/getmoon/flutter_env.git
        if [ $? -ne 0 ]
        then
            echo "clone flutter_env failed!"
            rm -rf flutter_env
            return 1
        fi
    fi

    return 0
}

function prepare_commandlinetools(){
    echo "prepare_commandlinetools"

    if [ -d "command-line-tools" ];then
        echo "command-line-tools exists, do not download again."
    else
        if [ ! -f "command-line-tools.zip" ];then
            echo "start download command-line-tools.zip"
            curl -o "command-line-tools.zip" https://repo.huaweicloud.com/harmonyos/ohpm/5.0.5/commandline-tools-linux-x64-5.0.5.310.zip

            # 判断是否下载成功
            if [ $? != 0 ];then
                echo "download command-line-tools.zip failed."
                rm -rf command-line-tools.zip
                return 1
            fi
        fi

        # 开始解压zip包
        echo "start unzip command-line-tools.zip"
        unzip -oqq "./command-line-tools.zip"

        echo "finish unzip command-line-tools.zip"
    fi

    # 开始替换API18 SDK
    rm -rf command-line-tools/sdk/default/openharmony/*
    curl -o command-line-tools/sdk/default/openharmony/sdk.tar.gz https://cidownload.openharmony.cn/version/Master_Version/OpenHarmony_5.1.0.102/20250403_020121/version-Master_Version-OpenHarmony_5.1.0.102-20250403_020121-ohos-sdk-full_5.1.0-Release.tar.gz
    tar -zxf command-line-tools/sdk/default/openharmony/sdk.tar.gz -C command-line-tools/sdk/default/openharmony/
    rm -rf command-line-tools/sdk/default/openharmony/manifest_tag.xml
    rm -rf command-line-tools/sdk/default/openharmony/windows
    rm -rf command-line-tools/sdk/default/openharmony/sdk.tar.gz

    mv command-line-tools/sdk/default/openharmony/linux/* command-line-tools/sdk/default/openharmony/
    rm -rf command-line-tools/sdk/default/openharmony/linux

    unzip command-line-tools/sdk/default/openharmony/ets-linux-x64-5.1.0.102-Beta1.zip -d command-line-tools/sdk/default/openharmony/
    unzip command-line-tools/sdk/default/openharmony/js-linux-x64-5.1.0.102-Beta1.zip -d command-line-tools/sdk/default/openharmony/
    unzip command-line-tools/sdk/default/openharmony/native-linux-x64-5.1.0.102-Beta1.zip -d command-line-tools/sdk/default/openharmony/
    unzip command-line-tools/sdk/default/openharmony/previewer-linux-x64-5.1.0.102-Beta1.zip -d command-line-tools/sdk/default/openharmony/
    unzip command-line-tools/sdk/default/openharmony/toolchains-linux-x64-5.1.0.102-Beta1.zip -d command-line-tools/sdk/default/openharmony/

    return 0
}

function prepare() {
    apt-get update && apt-get install -y python3 unzip make ninja-build pkg-config tree tar curl
	
    rm -rf ndk
    rm -rf src
	
    prepare_depot_tools
    if [ $? -ne 0 ]
    then
        echo "prepare_depot_tools failed!!"
        return 1
    fi
	
    prepare_flutter_engine_src
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_engine_src failed!!"
        return 1
    fi
	
    prepare_flutter_engine_third_party
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_engine_third_party failed!!"
        return 1
    fi
	
    prepare_flutter_git_bak
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_git_bak failed!!"
        return 1
    fi
	
    prepare_flutter_vpython
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_vpython failed!!"
        return 1
    fi

    prepare_flutter_engine_patch
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_engine_patch failed!!"
        return 1
    fi
	
    prepare_flutter_env
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_env failed!!"
        return 1
    fi

    prepare_commandlinetools
    if [ $? -ne 0 ]
    then
        echo "prepare_commandlinetools failed!!"
        return 1
    fi
	
    #创建npmrc文件
    rm -f ~/.npmrc
    echo -e "registry=https://repo.huaweicloud.com/repository/npm/\n@ohos:registry=https://repo.harmonyos.com/npm/\nlockfile=false" > ~/.npmrc
    rm -rf ~/.ohpm
    mkdir ~/.ohpm
    touch ~/.ohpm/.ohpmrc
    echo -e "registry=https://ohpm.openharmony.cn/ohpm/" > ~/.ohpm/.ohpmrc
    
    # 删除hvigor缓存
    rm -rf ~/.hvigor

    python3 src/flutter/attachment/scripts/ohos_setup_pre.py && ./setup.sh
    echo "--> Finish prepare.sh ."

    return 0
}

prepare $@

exit $?

#eof
